package com.buaa.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

public class BMapUtils {

	public static void main(String[] args) {
		//String addr = "黑龙江省哈尔滨市";
		//String addr = "黑龙江省齐齐哈尔市";
		//String addr = "黑龙江省鸡西市";
		//String addr = "黑龙江省鹤岗市";
		//String addr = "黑龙江省双鸭山市";
		//String addr = "黑龙江省大庆市";
		//String addr = "黑龙江省伊春市";
		//String addr = "黑龙江省佳木斯市";
//		String addr = "黑龙江省七台河市";
		//String addr = "黑龙江省牡丹江市";
//		String addr = "黑龙江省黑河市";
//		String addr = "黑龙江省绥化市";
		//String addr = "黑龙江省大兴安岭地区";
		String addr = "黑龙江省";
		Map<String, Float> map = getLatAndLngByAddress(addr);
		System.out.println(map.get("lng") + " " + map.get("lat"));
	}

	/**
	 * 批量通过地址获取经纬度
	 * 
	 * @param addrs
	 * @return
	 */
	public static Map<Long, Map<String, Float>> getLatAndLngByAddress(Map<Long, String> addrs) {
		Map<Long, Map<String, Float>> ret = new HashMap<Long, Map<String, Float>>();
		Map<String, Float> value = null;
		for (Entry<Long, String> addr : addrs.entrySet()) {
			addr.getValue();
			value = getLatAndLngByAddress(addr.getValue());
			ret.put(addr.getKey(), value);
		}
		return ret;
	}

	/**
	 * 通过地址获取经纬度
	 * 
	 * @param addr
	 * @return
	 */
	public static Map<String, Float> getLatAndLngByAddress(String addr) {
		String address = "";
		String lat = "0";
		String lng = "0";
		try {
			address = java.net.URLEncoder.encode(addr, "UTF-8");
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
		String url = String.format(
				"http://api.map.baidu.com/geocoder/v2/?" + "ak=4rcKAZKG9OIl0wDkICSLx8BA&output=json&address=%s",
				address);
		URL myURL = null;
		URLConnection httpsConn = null;
		// 进行转码
		try {
			myURL = new URL(url);
		} catch (MalformedURLException e) {

		}
		try {
			httpsConn = (URLConnection) myURL.openConnection();
			if (httpsConn != null) {
				InputStreamReader insr = new InputStreamReader(httpsConn.getInputStream(), "UTF-8");
				BufferedReader br = new BufferedReader(insr);
				String data = null;
				if ((data = br.readLine()) != null) {
					/**
					{
					    "status": 0,
					    "result": {
					        "location": {
					            "lng": 119.9529153641618,
					            "lat": 31.680905522300126
					        },
					        "precise": 0,
					        "confidence": 50,
					        "level": "UNKNOWN"
					    }
					}
					**/
					// int start = data.indexOf("\"lat\":") +
					// ("\"lat\":").length();
					// int end = data.indexOf("},\"precise\"");
					// if (start >= 0 && start <= end)
					// lat = data.substring(start, end);
					// start = data.indexOf("\"lng\":") + ("\"lng\":").length();
					// end = data.indexOf(",\"lat\"");
					// if (start >= 0 && start <= end)
					// lng = data.substring(start, end);

					Map<?, ?> map = JacksonUtil.readValue(data, Map.class);
					if (map != null) {
						Map<?, ?> result = (Map<?, ?>) map.get("result");
						if (result != null) {
							Map<?, ?> location = (Map<?, ?>) result.get("location");
							if (location != null) {
								lng = location.get("lng").toString();
								lat = location.get("lat").toString();
							}
						}
					}
				}
				insr.close();
			}
		} catch (IOException e) {
			System.out.println("exception");
		}
		Map<String, Float> map = new HashMap<String, Float>();
		map.put("lat", new Float(lat));
		map.put("lng", new Float(lng));
		return map;
	}
}
